// @charset "utf-8";
/**
 * 个股涨跌view
 */
define(function (require, exports, module) {
(async()=>{ 
	var ggzdModel = require('./ggzdmodel');
	var ggzdChart = require('./ggzdchart');

	// var autoLoop = require('../base/autoloop');
	var ggzdView = {
		date : '',
		init : async function () {
			var self = this;
			var tradingDay = await commonMethod.getTradingDay();
			if (tradingDay == 0) {
				var getTradeDayTimer = setTimeout(function () {
					self.init();
				}, 200);
				return;
			} else {
				clearTimeout(getTradeDayTimer);
			}
			self.date = tradingDay;
			self.requestData();
			self.renderRisingProb(tradingDay);
			interactions.datePicker(tradingDay, 'ggzd', function (date) {
				self.date = date;
				self.requestData();
				self.renderRisingProb(date);
			});
		},
		autoRefresh : async function (isIn) {
			var self = this;
			var curtime = await commonMethod.serverTime();
			var isTradeTime = commonMethod.thsQuote.isTradeTime('1A0001');
			if (($('.tab-active').index() == 0 && curtime == self.date && isTradeTime) || isIn) {
				self.requestData();
				self.renderRisingProb(self.date);
			}
		},
		requestData :async  function () {
			var self = this;
			await ggzdModel.request({
				market: 'USHA;USZA',
				date: self.date,
				datatype: 'zhangfustats'
			}, function (result) {
				// alert(result);
				self.analyticalData(result);
			});
			$('#view_input_ggzddate').val(commonMethod.dateSplit(self.date));
		},
		// ztData : [],
		// dtData : [],
		// fyzztData : [],
		// newZtData : '--',
		// newDtData : '--',
		// newFyzztData : '--',
		analyticalData : async function (result) {
			var self = this;
			var sum = 0;
			for (var i = 0; i < result.length; i++) {
				sum += result[i];
			}
			var zdtjDataTotal = 0,
				zdtjRiseData = 0,
				zdtjFalldata = 0,
				zdtjRating = 0;
			var curtime = await commonMethod.serverTime();
			if(result.length > 0 && sum !== 0){
				for (var i in result) {
					if (i < 10) {
						if (i < 5) {
							zdtjRating += result[i];
						}
						zdtjRiseData += result[i];
					} else {
						zdtjFalldata += result[i];
					}
					zdtjDataTotal += result[i];
				}
				// if (self.date == curtime && await commonMethod.judgeIntime(['08:50', '09:45'])) {
				// 	$('#view_a_readingbtn').addClass('cannot-read');
				// } else {
				$('#view_a_readingbtn').removeClass('cannot-read');
				// }
			} else {
				$('#view_a_readingbtn').addClass('cannot-read');
				zdtjDataTotal = 0;
				zdtjRiseData = '--';
				zdtjFalldata = '--';
				zdtjRating = '--';
				result = [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null];
			}
			self.renderReadText(zdtjRating);
			ggzdChart.render(zdtjDataTotal, zdtjRiseData, zdtjFalldata, result);
		},
		renderReadText : function (zdtjRating) {
			var ratingText = '--';
			if (zdtjRating !== '--') {
				if (zdtjRating >= 300) {
					ratingText = '强';
				} else if (zdtjRating >= 150 && zdtjRating < 300) {
					ratingText = '较强';
				} else if (zdtjRating >= 80 && zdtjRating < 150) {
					ratingText = '较弱';
				} else if (zdtjRating >= 0 && zdtjRating < 80) {
					ratingText = '弱';
				}
				var ratingImg = 1;
				if(zdtjRating <= 40){
					ratingImg = 1;
				}else if(zdtjRating <= 80){
					ratingImg = 2;
				}else if(zdtjRating <= 120){
					ratingImg = 3;
				}else if(zdtjRating <= 150){
					ratingImg = 4;
				}else if(zdtjRating <= 220){
					ratingImg = 5;
				}else if(zdtjRating <= 300){
					ratingImg = 6;
				}else if(zdtjRating <= 500){
					ratingImg = 7;
				}else{
					ratingImg = 8;
				}
				$('#reading-ggzdimg').attr('src', 'images/unscramble3-'+ ratingImg +'.jpg');
			}
			$('#view_strong_ggzdread').text(ratingText);
		},

		renderRisingProb : async function (startDate) {
			var serverTime = await commonMethod.serverTime();
			var type = 'zdtjpjhc';
			if (serverTime == startDate && await commonMethod.judgeIntime(['00:01', '09:45'])) {
				interactions.renderRisingProb(type, 'NUL');
			} else {
				interactions.requestZxData(type, startDate);
			}
		}
	};
	module.exports = ggzdView;
})();
});